package co.yixiang.hw.service.impl;

import cn.hutool.core.date.DateUtil;
import co.yixiang.hw.domain.YxStoreCoupon;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.hw.repository.YxStoreCouponRepository;
import co.yixiang.hw.service.YxStoreCouponService;
import co.yixiang.hw.service.dto.YxStoreCouponDto;
import co.yixiang.hw.service.dto.YxStoreCouponQueryCriteria;
import co.yixiang.hw.service.mapper.YxStoreCouponMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.annotation.Resource;


/**
* @author Walert
* @date 2020-03-10
*/
@Service
@CacheConfig(cacheNames = "yxStoreCoupon")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCouponServiceImpl implements YxStoreCouponService {

    @Resource
    private YxStoreCouponRepository yxStoreCouponRepository;
    @Resource
    private YxStoreCouponMapper yxStoreCouponMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxStoreCouponQueryCriteria criteria, Pageable pageable){
//        Page<YxStoreCoupon> page = yxStoreCouponRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Long time  = DateUtil.parseDate(DateUtil.today()).getTime() /1000L;
        Page<Map> page = yxStoreCouponRepository.getCouponList(criteria.getUid(),time.intValue(), pageable);
        return PageUtil.toPageHadTotalPage(page.getContent(),page.getTotalElements(),page.getTotalPages());
    }

    @Override
    //@Cacheable
    public List<YxStoreCouponDto> queryAll(YxStoreCouponQueryCriteria criteria){
        return yxStoreCouponMapper.toDto(yxStoreCouponRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "#p0")
    public YxStoreCouponDto findById(Integer id) {
        YxStoreCoupon yxStoreCoupon = yxStoreCouponRepository.findById(id).orElseGet(YxStoreCoupon::new);
        ValidationUtil.isNull(yxStoreCoupon.getId(),"YxStoreCoupon","id",id);
        return yxStoreCouponMapper.toDto(yxStoreCoupon);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxStoreCouponDto create(YxStoreCoupon resources) {
        return yxStoreCouponMapper.toDto(yxStoreCouponRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreCoupon resources) {
        YxStoreCoupon yxStoreCoupon = yxStoreCouponRepository.findById(resources.getId()).orElseGet(YxStoreCoupon::new);
        ValidationUtil.isNull( yxStoreCoupon.getId(),"YxStoreCoupon","id",resources.getId());
        yxStoreCoupon.copy(resources);
        yxStoreCouponRepository.save(yxStoreCoupon);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxStoreCouponRepository.deleteById(id);
        }
    }
}